package vn.gameloft.blockbreaker.model;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Circle;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Disposable;

public class Block extends Rectangle implements Disposable {
    private static final long serialVersionUID = 3884834249081958911L;
    private Block next = null;
    private Block prev = null;

    @Override
    public void dispose() {
        if (this.prev != null) {
            this.prev.setNext(this.next);
        }
    }

    public void setNext(Block next) {
        this.next = next;
        if (next != null) {
            next.prev = this;
        }
    }

    public Block getNext() {
        return this.next;
    }

    public Block getPrev() {
        return this.prev;
    }

    public boolean isOverlap(float x, float y, float w, float h) {
        if (this.x > x + w || this.x + this.width < x || this.y > y + h
                || this.y + this.height < y)
            return false;

        return true;
    }

    public boolean isOverlap(Rectangle r) {
        return isOverlap(r.x, r.y, r.width, r.height);
    }

    public boolean isOverlap(Circle c) {
        return isOverlap(c.x - c.radius, c.y - c.radius, 2 * c.radius,
                2 * c.radius);
    }

    public void draw() {
        ShapeRenderer brush = ShapeFactory.getInstance();
        brush.begin(ShapeType.Filled);
        brush.setColor(Color.MAGENTA);
        brush.rect(this.x, this.y, this.width, this.height);
        brush.end();
        brush.begin(ShapeType.Line);
        brush.setColor(Color.GREEN);
        brush.rect(this.x, this.y, this.width, this.height);
        brush.end();
    }
}
